import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/im/page/sub/TransferMessagePage.dart';
import 'package:im_app/im/page/view/ListSeparator.dart';

import '../../dao/ImSessionDao.dart';
import '../../pojo/ImPojo.dart';
import '../../util/DateUtil.dart';
import '../../util/ImConstant.dart';
import '../bloc/CollectListCubit.dart';
import '../bloc/TransferMessageCubit.dart';
import '../view/component/pane/ImMessageHandler.dart';
import '../view/component/pane/MessagePane.dart';
import 'ImCollectListPage.dart';

class ImCollectInfoPage extends StatelessWidget {
  final ImCollect collect;

  const ImCollectInfoPage({required this.collect, super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _buildAppBar(context),
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      body: _buildBody(context),
    );
  }

  AppBar _buildAppBar(BuildContext context) {
    return AppBar(
      backgroundColor: ImConstant.IM_MAIN_BACKGROUND_COLOR,
      leading: GestureDetector(
        onTap: () {
          Navigator.of(context).pop();
        },
        child: const Icon(Icons.chevron_left),
      ),
      title: _buildTitleName(),
      centerTitle: true,
      actions: [
        IconButton(
          icon: const Icon(
            Icons.more_horiz,
            color: ImConstant.IM_TEXT_COLOR,
          ),
          tooltip: '',
          onPressed: () {
            showModalBottomSheet(
                context: context,
                builder: (BuildContext context) {
                  return Container(
                    height: 170,
                    decoration: const BoxDecoration(
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(10), topRight: Radius.circular(10))),
                    child: Column(
                      children: [
                        ListTile(
                          title: const Center(child: Text('发送给朋友')),
                          titleAlignment: ListTileTitleAlignment.center,
                          onTap: () async {
                            var session = collect.message?.imSession;
                            if (session == null) {
                              var sessionDao = ImSessionDao();
                              session = await sessionDao.getByServerIdAndUserId(collect.sessionId, collect.userId);
                              collect.message!.imSession = session;
                            }
                            Navigator.of(context).push(MaterialPageRoute(builder: (context) {
                              return BlocProvider(
                                create: (BuildContext context) {
                                  return TransferMessageCubit([collect.message!], TransferType.simple, TransferScene
                                    (sceneType: TransferSceneType.collect))
                                    ..loadData([collect.message!], TransferType.simple, TransferScene(sceneType: TransferSceneType.collect));
                                },
                                child: const TransferMessagePage(),
                              );
                            }));
                          },
                        ),
                        ListSeparator(indent: 10),
                        ListTile(
                          title: const Center(child: Text('删除')),
                          titleAlignment: ListTileTitleAlignment.center,
                          onTap: () async {
                            var messageHandler = ImMessageHandler();
                            await messageHandler.deleteCollect(collect);
                            // Navigator.of(context).pushNamedAndRemoveUntil("/CollectListPage", ModalRoute.withName('/MainPage'));
                            Navigator.of(context).pushAndRemoveUntil(
                                MaterialPageRoute(
                                    builder: (context) {
                                      return BlocProvider(
                                        create: (context) => CollectListCubit(),
                                        child: const ImCollectListPage(),
                                      );
                                    },
                                    settings: const RouteSettings(name: '/CollectListPage')),
                                ModalRoute.withName('/MainPage'));
                          },
                        ),
                        ListSeparator(
                          indent: 10,
                        ),
                        ListTile(
                          title: const Center(child: Text('取消')),
                          titleAlignment: ListTileTitleAlignment.center,
                          onTap: () {
                            Navigator.of(context).pop();
                          },
                        ),
                        // ListTile(title: Text('发送给朋友'), titleAlignment: ListTileTitleAlignment.center, onTap: () {},),
                        // ListTile(title: Text('发送给朋友'), titleAlignment: ListTileTitleAlignment.center, onTap: () {},),
                      ],
                    ),
                  );
                });
          },
        )
      ],
    );
  }

  Widget _buildBody(BuildContext context) {
    Size scrSize = MediaQuery.of(context).size;
    double screenWidth = scrSize.width - 30;
    return Container(
      color: ImConstant.IM_RESERVE_BACKGROUND_COLOR,
      padding: const EdgeInsets.only(left: 10, right: 10, top: 10),
      alignment: AlignmentDirectional.topStart,
      child:
          MessageRefPane(message: collect.message!, param: MessageRefParam(maxLines: 100, refMaxLength: screenWidth)),
    );
  }

  Widget _buildTitleName() {
    return Column(
      children: [
        const Text('详情'),
        Row(mainAxisAlignment: MainAxisAlignment.center, children: [
          Text(
            "来自${collect?.message?.fromUserId ?? ''}",
            style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
          ),
          const SizedBox(
            width: 20,
          ),
          Text(
            DateUtil.formatDate(collect?.createTime) ?? '',
            style: const TextStyle(fontSize: ImConstant.IM_SMALL_SIZE),
          )
        ])
      ],
    );
  }
}
